package com.easypreview.preview.persist.channel.impl;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.springframework.stereotype.Repository;

import com.easypreview.preview.domain.channel.Channel;
import com.easypreview.preview.persist.PMF;
import com.easypreview.preview.persist.channel.ChannelRepository;
import com.easypreview.preview.util.page.Page;

/**
 * 频道持久化实现类（基于JDO）
 * @author ajian
 * 
 */
@Repository
public class ChannelRepositoryImpl implements ChannelRepository {

	@Override
	public Channel queryUniqueByPath(String path) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Channel channel;
		Query q = pm.newQuery(Channel.class);
		q.setFilter("path == param");
		q.declareParameters("String param");
		q.setUnique(true);
		try {
			channel = pm.detachCopy( (Channel)q.execute(path));
		} finally {
			q.closeAll();
			pm.close();
		}
		return channel;
	}

	@Override
	public Channel queryUniqueByName(String name) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Channel channel;
		Query q = pm.newQuery(Channel.class);
		q.setFilter("name == param");
		q.declareParameters("String param");
		q.setUnique(true);
		try {
			channel =  pm.detachCopy( (Channel)q.execute(name));
		} finally {
			q.closeAll();
			pm.close();
		}
		return channel;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Page<Channel> queryPage(Page<Channel> page) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<Channel> channels = new ArrayList<Channel>(),results=null;
		Query q = pm.newQuery(Channel.class);
		q.setRange((page.getPageNo() - 1) * page.getPageSize() + 1,
				page.getPageNo() * page.getPageSize() + 1);
		try{
			results = (List<Channel>)q.execute();
			for(Channel channel: results){
				channels.add(pm.detachCopy(channel));
			}
		}finally{
			q.closeAll();
			pm.close();
		}
		page.setResult(channels).setTotalCount(channels.size());
		return page;
	}

	@Override
	public Channel get(String id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Channel channel;
		Query q = pm.newQuery(Channel.class);
		q.setFilter("id == param");
		q.declareParameters("String param");
		q.setUnique(true);
		try {
			channel =  pm.detachCopy( (Channel) q.execute(id));
		} finally {
			q.closeAll();
			pm.close();
		}
		return channel;
	}

	@Override
	public void save(Channel channel) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(channel);
		} finally {
			pm.close();
		}

	}

	@Override
	public void update(Channel channel) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Channel newChannel = pm.getObjectById(Channel.class,
					channel.getKey());
			newChannel.setAnchorText(channel.getAnchorText());
			newChannel.setAttributes(channel.getAttributes());
			newChannel.setDate(channel.getDate());
			newChannel.setFatherId(channel.getFatherId());
			newChannel.setHotKeyWord(channel.getHotKeyWord());
			newChannel.setId(channel.getId());
			newChannel.setMetaDescr(channel.getMetaDescr());
			newChannel.setMetaKeyWord(channel.getMetaKeyWord());
			newChannel.setMetaTitle(channel.getMetaTitle());
			newChannel.setName(channel.getName());
			newChannel.setPath(channel.getPath());
			newChannel.setSort(channel.getSort());
			newChannel.setTags(channel.getTags());
			newChannel.setTitle(channel.getTitle());
		} finally {
			pm.close();
		}

	}

	@Override
	public void delete(String id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery(Channel.class,"id == "+id+"");
		try {
			Channel channel = (Channel)q.execute();
			pm.deletePersistent(channel);
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Channel> queryTop() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<Channel> results = new ArrayList<Channel>();
		Query q = pm.newQuery(Channel.class);
		q.setFilter("fatherId == param");
		q.declareParameters("String param");
		try {
			results = (List<Channel>) pm.detachCopy(q.execute(""));			
		} finally {
			q.closeAll();
			pm.close();
		}
		return results;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Page<Channel> queryPageTop(Page<Channel> page) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<Channel> channels = new ArrayList<Channel>(),results=null;
		Query q = pm.newQuery(Channel.class, "fatherId == \"\" ");
		q.setRange((page.getPageNo() - 1) * page.getPageSize() + 1,
				page.getPageNo() * page.getPageSize() + 1);				
		try{
			results = (List<Channel>) q.execute();
			for(Channel channel: results){
				channels.add(pm.detachCopy(channel));
			}
		}finally{
			q.closeAll();
			pm.close();
		}
		page.setResult(channels).setTotalCount(channels.size());
		return page;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Channel> query(Object object) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<Channel> channels = new ArrayList<Channel>(),results=null;
		Query q = pm.newQuery(Channel.class);
		try {
			results = (List<Channel>) q.execute();
			for(Channel channel : results){
				channels.add(pm.detachCopy(channel));
			}
		} finally {
			q.closeAll();
			pm.close();
		}
		return channels;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Channel> queryChildById(String id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<Channel> channels = new ArrayList<Channel>(),results=null;
		Query q = pm.newQuery(Channel.class, "fatherId == " + id + " ");
		try {
			results = (List<Channel>) q.execute("");
			for(Channel channel : results){
				channels.add(pm.detachCopy(channel));
			}
		
		} finally {
			q.closeAll();
			pm.close();
		}
		return channels;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Page<Channel> queryPageChildById(Page<Channel> page, String id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<Channel> channels = new ArrayList<Channel>(),results = null;
		Query q = pm.newQuery(Channel.class, "fatherId == " + id + " ");
		q.setRange((page.getPageNo() - 1) * page.getPageSize() + 1,
				page.getPageNo() * page.getPageSize() + 1);
		try{
			results = (List<Channel>) q.execute();
			for(Channel channel : results){
				channels.add(pm.detachCopy(channel));
			}
		}finally{
			q.closeAll();
			pm.close();
		}		
		page.setResult(channels).setTotalCount(channels.size());
		return page;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Channel> queryChannels() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<Channel> channels = new ArrayList<Channel>(),results=null;
		Query q = pm.newQuery(Channel.class);
		q.setOrdering("date desc");
		try {
			results = (List<Channel>)q.execute();            
	        for(Channel channel : results){
	            channels.add(pm.detachCopy(channel));
	        }
		} finally {
			q.closeAll();
			pm.close();
		}
		return channels;
	}

	@Override
	public Channel getFatherByFatherId(String fatherId) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Channel father;
		Query q = pm.newQuery(Channel.class);
		q.setFilter("fatherId == param");
		q.declareParameters("String param");
		q.setUnique(true);
		try {
			father=pm.detachCopy((Channel) q.execute(fatherId));
		} finally {
			q.closeAll();
			pm.close();
		}
		return father;
	}

}
